<?php
include "../../site_specific/project.php";
include "../../includes/constants.php";
include "../../site_specific/external_constants.php";
include "../../includes/phpmailer/class.phpmailer.php";
include "../../includes/phpmailer/class.smtp.php";
include "../../includes/utilities.php";
include "../../includes/pages.php";
include "../../includes/database.php";
include "../../includes/users.php";
include "../../includes/date.php";
include "../../includes/text.php";
include "../../includes/files.php";
include "../../includes/tableproperties.php";
include "../../includes/shopping_cart.php";
include "../../includes/init_front.php";

# Establish database connection
establishDatabaseConnection();

#Get the id of moved item
$id = $_GET["id"];

#if action is up,it means the moved item is moving up otherwise is moving down.
$action = $_GET["action"];

$parent = $_GET["parent"];

$section = $_GET["section"];

#get page object whose id is $id
$o_PAGE = getPageDetails($id);

#Get children list whose parent is the $id`s parent
$current_data_items = $PAGES -> getChildrenOfPage($o_PAGE-> getData("p_parent"));

#The id which is selected to move 
$selectedID = $id;

#The sort of page which is selected to move
$selectedsort = $o_PAGE-> getData("p_sort");

#initialize the id of page which will be replaced by selected page
$relpacedID = "";

#The sort of page which will be replaced by selected page.
$relpacedsort = 0;
	
if($action == "up"){	

	#initialize the page array
	$array_page = $current_data_items -> getPagesArray();
	
	#the sort of selected page
	$sort_selected = $selectedsort;

	#Get the index of page in the page list according to given id
	$index_selected = $current_data_items -> getPageIndexViaId($array_page,$selectedID);
	
	#If the selected item is the first one of page list array, it will not do anything, otherwise it will start the move functions
	if($index_selected != 0){
		
		#Get the uppder page object according to the given index.
		$ob_up  = $current_data_items ->getPageViaIndex($array_page,$index_selected-1);
		
		#Get the sort of the upper page
		$sort_up = $ob_up -> getData('p_sort');
		
		#normal gap between upper item and selected item. 
		if($sort_selected-$sort_up==1){
			
			$id_selected = $id;
				
			$id_replace = $ob_up -> getData('p_id');
				
			$sort_replace = $sort_selected;	
					
			$sort_selected = $sort_up;
			
		}
		#If the gap is bigger or smaller
		else{		
			$id_selected = $id;
				
			$id_replace = $ob_up -> getData('p_id');
				
			$sort_replace = $sort_up+1;
						
			$sort_selected = $sort_up;
		}
		
		#update script
		$sql_update_replaced = "UPDATE ".$PROJECT_DB_TABLES["pages"]." SET p_sort=".$sort_replace." WHERE p_id=\"".$id_replace."\"";
		
		executeSQLQuery($sql_update_replaced);
		
		$sql_update_selected = "UPDATE ".$PROJECT_DB_TABLES["pages"]." SET p_sort=".$sort_selected." WHERE p_id=\"".$id_selected."\"";
		
		executeSQLQuery($sql_update_selected);

	}
	else{

	}
}
else if($action=="down"){
	
	#initialize the page array
	$array_page = $current_data_items -> getPagesArray();
	
	#the sort of selected page
	$sort_selected = $selectedsort;
	
	#Get the index of page in the page list according to given id
	$index_selected = $current_data_items -> getPageIndexViaId($array_page,$selectedID);
	
	#If the selected item is the end one of page list array, it will not do anything, otherwise it will start the move functions
	if($index_selected != count($array_page)-1){
		
		#Get the next page object according to the given index.
		$ob_down  = $current_data_items ->getPageViaIndex($array_page,$index_selected+1);
		
		#Get the sort of the next page
		$sort_down = $ob_down -> getData('p_sort');
		
		#normal gap between next item and selected item. 
		if($sort_down-$sort_selected==1){
			
			$id_selected = $id;
				
			$id_replace = $ob_down -> getData('p_id');
				
			$sort_replace = $sort_selected;	
					
			$sort_selected = $sort_down;
			
		}
		#If the gap is bigger or smaller
		else{		
			$id_selected = $id;
				
			$id_replace = $ob_down -> getData('p_id');
				
			$sort_replace = $selectedsort+1;
						
			$sort_selected = $selectedsort;
		}
		#update script
		$sql_update_replaced = "UPDATE ".$PROJECT_DB_TABLES["pages"]." SET p_sort=".$sort_replace." WHERE p_id=\"".$id_replace."\"";
		
		executeSQLQuery($sql_update_replaced);
		
		$sql_update_selected = "UPDATE ".$PROJECT_DB_TABLES["pages"]." SET p_sort=".$sort_selected." WHERE p_id=\"".$id_selected."\"";
		
		executeSQLQuery($sql_update_selected);

	}
	else{

	}

}

#Refresh after update
$PAGES = getAllPages();

#table start tag
$output = "<table cellspacing=\"0\" align=\"left\">";

#Regrab the refreshed list of parameter $id
$data_after_refresh = $PAGES -> getChildrenOfPage($o_PAGE-> getData("p_parent"));

#gerenate the list of pages
foreach($data_after_refresh -> getPageIDs() as $id_menulist){
	
	$ob_menulist = $data_after_refresh -> getPage($id_menulist);
	
	$data_childrenlist = $PAGES -> getChildrenOfPage($id_menulist);
	
	$array_childrenlist = $data_childrenlist -> getPagesArray();
	
	#If the current page have children, it will show document icon, otherwith folder icon will be replaced
	if(count($array_childrenlist) == 0){
		
		$output .= "<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\""."img_".$id_menulist."\" src=\"../assets/images/tree/document.png\");\"/></td>";
		
		$output .= "<td style=\"border-color: white;border-style: solid;\" align=\"left\"><a id=\"a_".$id_menulist."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$id_menulist."','".$ob_menulist -> getData("p_parent")."','".$ob_menulist -> getData("p_section")."');\" style=\"text-decoration:none;\">".$ob_menulist -> getData("p_name")."(".$id_menulist.")"."</a><br/><div align=\"left\" id=\""."div".$id_menulist."\"></div></td></tr>";
	}
	else{
		
		$output .= "<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\""."img_".$id_menulist."\" src=\"../assets/images/tree/folder_closed.png\");\"/></td>";
		
		$output .= "<td style=\"border-color: white;border-style: solid;\" align=\"left\"><a id=\"a_".$id_menulist."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$id_menulist."','".$ob_menulist -> getData("p_parent")."','".$ob_menulist -> getData("p_section")."');\" style=\"text-decoration:none;\">".$ob_menulist -> getData("p_name")."(".$id_menulist.")"."</a><br/><div align=\"left\" id=\""."div".$id_menulist."\"></div></td></tr>";
	}
}

#table end tag
$output .= "</table>";

#generate output data
echo $output;

?>